/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var minOperations = function(nums, k) {
    nums = nums.sort((a,b) => a-b)
    console.log('sort', nums)
    let result = 0
    while (nums.length >= 2 && nums[0]<k) {
        result++
        let min = nums[0] * 2 + nums[1]
        nums.shift()
        nums.shift()
        console.log('shift', nums)
        // 不能push排序，开销太大
        // nums.push(min)
        // nums = nums.sort((a,b) => a-b)
        if (min > nums[nums.length-1]) {
            nums.push(min)
        } else {
            for (let i=0;i<nums.length;i++) {
                if (min<=nums[i]) {
                    nums.splice(i,0,min)
                    break;
                }
            }
        }
        console.log('sort', nums)
    }
    return result
};
const nums = [1,1,2,4,9]
const k = 20
console.log(minOperations(nums, k))